<?php

namespace App\Manage;

use App\Service\Base\Manage;

class SiteAdvManage extends Manage
{
    // 广告
    public function initialize()
    {
        parent::initialize();

        $this->model = 'SiteAdv';
    }

    // 父级
    // 添加，修改，删除，列表，停用 操作

    // 软删除
    // 启用、停用

    // 广告位
    public function space($is_change = true)
    {
        $space = config('app.adv_space');

        // 改变
        if($is_change){
            foreach($space as $key => $item){
                $result[] = ['title' => $item, 'id' => $key, 'number' => $lists[$key] ?? 0];
            }
            $space = $result;
        }

        return $space;
    }

    // 列表查询
    protected function listWhere()
    {
        $opt      = $this->option();
        $space    = $opt->getData('space', 0, 'intval');
        $cate_id  = $opt->getData('cate_id', 0, 'intval');
        $terminal = $opt->getData('terminal', null, 'trim');

        $where = [['status', '>=', 0]];
        $bang  = [];
        if($terminal){
            $bang[]  = 'terminal';
            $where[] = ['terminal', '=', $terminal];
        }
        if($space){
            $bang[]  = 'space';
            $where[] = ['space', '=', $space];
        }
        if($cate_id){
            $bang[]  = 'cate_id';
            $where[] = ['cate_id', '=', $cate_id];
        }

        // 缓存
        $this->bang = $bang;

        return $where;
    }

    // 添加
    protected function _before_add()
    {
        $opt  = $this->option();
        $data = [
            'title'    => $opt->getData('title', null, 'trim'),
            'space'    => $opt->getData('space', null, 'trim'),
            'rank'     => $opt->getData('rank', 0, 'intval'),
            'pic'      => $opt->getData('pic', null, 'trim'),
            'url'      => $opt->getData('url', null, 'trim'),
            'terminal' => $opt->getData('terminal', 'app', 'trim'),
            'status'   => $opt->getData('status', 1, 'intval'),
            //'cate_id'  => $opt->getData('cate_id', 0, 'intval'),
            //'describe' => $opt->getData('describe', null, 'trim'),
        ];


        return $data;
    }

    // 修改
    protected function _before_edit()
    {
        $opt  = $this->option();
        $data = [
            'title'    => $opt->getData('title', null, 'trim'),
            'space'    => $opt->getData('space', null, 'trim'),
            'rank'     => $opt->getData('rank', 0, 'intval'),
            'pic'      => $opt->getData('pic', null, 'trim'),
            'url'      => $opt->getData('url', null, 'trim'),
            'terminal' => $opt->getData('terminal', 'app', 'trim'),
            //'status'   => $opt->getData('status', 1, 'intval'),
        ];


        return $data;
    }

}


